package com.qire.manhua.model.viewModel.impl;

import android.graphics.Typeface;
import android.widget.TextView;

import com.qire.antsbinder.viewModel.ViewModelFactory;
import com.qire.antsrouter.AntsRouter;
import com.qire.antsrouter.card.RoomCard;
import com.qire.common.constant.Navigation;
import com.qire.common.support.base.BaseViewModel;
import com.qire.common.support.base.IViewModel;
import com.qire.common.widget.adapter.pager.SimpleFragmentPagerAdapter;
import com.qire.common.widget.adapter.pager.SimpleFragmentPagerAdapter.FragmentWrapper;
import com.qire.manhua.databinding.FragmentBookshelfBinding;
import com.qire.manhua.fragment.BookshelfFragment;
import com.qire.manhua.model.enumeration.PageTabIndex;

import androidx.fragment.app.Fragment;
import androidx.lifecycle.MutableLiveData;

/**
 * 首页-书架页
 */
public class BookshelfViewModel extends BaseViewModel<IViewModel> {

    private BookshelfFavoritesViewModel favoritesViewModel;
    private BookshelfHistoryViewModel historyViewModel;
    private BookshelfDownloadViewModel downloadViewModel;

    private int currentPageNo;

    //region    #附加管理 begin
    /**
     * 附加属性绑定
     */
    public void onAddition(BookshelfFragment fragment, int pageTabIndex) {

        favoritesViewModel = ViewModelFactory.crate(fragment.getActivity(), BookshelfFavoritesViewModel.class);
        historyViewModel = ViewModelFactory.crate(fragment.getActivity(), BookshelfHistoryViewModel.class);
        downloadViewModel = ViewModelFactory.crate(fragment.getActivity(), BookshelfDownloadViewModel.class);

        Fragment favoritesFragment = AntsRouter.ROUTER.<RoomCard>buildCard(Navigation.Fragment.BookshelfFavoritesFragment)
                .withData("viewModel", favoritesViewModel)
                .get();
        Fragment historyFragment = AntsRouter.ROUTER.<RoomCard>buildCard(Navigation.Fragment.BookshelfHistoryFragment)
                .withData("viewModel", historyViewModel)
                .get();
        Fragment downloadFragment = AntsRouter.ROUTER.<RoomCard>buildCard(Navigation.Fragment.BookshelfDownloadFragment)
                .withData("viewModel", downloadViewModel)
                .get();

        SimpleFragmentPagerAdapter bookshelfPagerAdapter = new SimpleFragmentPagerAdapter(fragment.getChildFragmentManager(), new FragmentWrapper[] {
            new FragmentWrapper("收藏", favoritesFragment),
            new FragmentWrapper("历史", historyFragment),
            new FragmentWrapper("下载", downloadFragment),
        });

        FragmentBookshelfBinding viewDataBinder = fragment.viewDataBinder();
        viewDataBinder.bookshelfViewPager.setAdapter(bookshelfPagerAdapter);
        viewDataBinder.bookshelfTabLayout.setViewPager(viewDataBinder.bookshelfViewPager);
        TextView tabText = (TextView) viewDataBinder.bookshelfTabLayout.getTabAt(pageTabIndex);
        tabText.setTextSize(20);
        tabText.setTypeface(Typeface.DEFAULT_BOLD);
    }

    /**
     * 清理函数，在Activity退出时，ViewModel生命周期到期会被回收，调用该事件，页面由于切换、横竖屏转换、内存回收等情况并不会影响到ViewModel的生命周期。
     */
    @Override
    protected void onCleared() {
        super.onCleared();
    }
    //endregion #附加管理 end

    public void setCurrentPageNo(int pageNo) {
        currentPageNo = pageNo;
    }

    /** 刷新可见子页 */
    public void refreshVisibleSubPage() {
        favoritesViewModel.loadCarouselData();
        favoritesViewModel.loadFavoritesData();
        historyViewModel.loadHistoryList();
        downloadViewModel.loadDownloadData();
    }

    /** 关闭编辑模式 */
    public void closeEditMode() {
        switch (currentPageNo) {
            case PageTabIndex.TAB_INDEX_BOOKSHELF_FAVORITES : {
                favoritesViewModel.toggleEditing(false);
                favoritesViewModel.setBookshelfViewModel(null);
                break;
            }
            case PageTabIndex.TAB_INDEX_BOOKSHELF_HISTORY : {
                historyViewModel.toggleEditing(false);
                historyViewModel.setBookshelfViewModel(null);
                break;
            }
            case PageTabIndex.TAB_INDEX_BOOKSHELF_DOWNLOAD : {
                downloadViewModel.toggleEditing(false);
                downloadViewModel.setBookshelfViewModel(null);
                break;
            }
        }
        gotoToolBarDisplayNo(2);
        toggleInterceptState(false);
    }

    /** 根据当前显示子页，切换该页的编辑状态 */
    public void toggleEditMode() {
        boolean nextEditState = getToValue("interceptState", false);
        switch (currentPageNo) {
            case PageTabIndex.TAB_INDEX_BOOKSHELF_FAVORITES : {
                nextEditState = favoritesViewModel.isAllowEdit();
                favoritesViewModel.toggleEditing(nextEditState);
                favoritesViewModel.setBookshelfViewModel(nextEditState ? this : null);
                break;
            }
            case PageTabIndex.TAB_INDEX_BOOKSHELF_HISTORY : {
                nextEditState = historyViewModel.isAllowEdit();
                historyViewModel.toggleEditing(nextEditState);
                historyViewModel.setBookshelfViewModel(nextEditState ? this : null);
                break;
            }
            case PageTabIndex.TAB_INDEX_BOOKSHELF_DOWNLOAD : {
                nextEditState = downloadViewModel.isAllowEdit();
                downloadViewModel.toggleEditing(nextEditState);
                downloadViewModel.setBookshelfViewModel(nextEditState ? this : null);
                break;
            }
        }
        gotoToolBarDisplayNo(nextEditState ? 1 : 2);
        toggleInterceptState(nextEditState);
    }

    /**
     * 跟据当前显示子页，切换该页所有子项选中状态
     */
    public void toggleCheckAll() {
        switch (currentPageNo) {
            case PageTabIndex.TAB_INDEX_BOOKSHELF_FAVORITES : {
                favoritesViewModel.toggleCheckAll();
                return;
            }
            case PageTabIndex.TAB_INDEX_BOOKSHELF_HISTORY : {
                historyViewModel.toggleCheckAll();
                return;
            }
            case PageTabIndex.TAB_INDEX_BOOKSHELF_DOWNLOAD : {
                downloadViewModel.toggleCheckAll();
                return;
            }
        }
    }

    public void toggleAllCheckUI(boolean isAllCheck) {
        set("allChecked", isAllCheck);
        set("allTextColor", isAllCheck ? "#ff9024" : "#5c5c5c");
    }

    public MutableLiveData<Boolean> getAllChecked() {
        return get("allChecked");
    }

    public MutableLiveData<String> getAllTextColor() {
        return get("allTextColor");
    }

    public void gotoToolBarDisplayNo(int displayNo) {
        set("toolBarDisplayNo", displayNo);
    }

    public MutableLiveData<Integer> getToolBarDisplayNo() {
        return get("toolBarDisplayNo");
    }

    public void gotoPageNo(int pageNo) {
        set("pageNo", pageNo);
        setCurrentPageNo(pageNo);
    }

    public MutableLiveData<Integer> getPageNo() {
        return get("pageNo");
    }

    private void toggleInterceptState(boolean interceptState) {
        set("interceptState", interceptState);
    }

    public MutableLiveData<Boolean> getInterceptState() {
        return get("interceptState");
    }

}
